#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int random(); /*返回生成的随机数*/
int judge();  /*判断答案是否正确*/
char random1();

double result = 0; /* 设置全局变量*/

int main()
{
    int i; /* 定义变量*/
    int num1, num2;
    char op;
    int yesorno;
    int count, wrongs = 0, score = 0;
    srand(time(NULL)); /* 初始化随机数种子 */
    for (i = 1; i <= 10; i++)
    {
        num1 = random(); /*获取第一个随机数*/
        num2 = random(); /*获取第二个随机数*/
        op = random1();/*获取随机的运算符*/

        printf("Question: %d %c %d\n", num1, op, num2);
        switch (op)  /*根据不同的运算符生成计算结果*/
        {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            result = (double)num1 / num2;
            break;
        }

        count = 0; /*每个等式重置count值，使每一个等式都有三次机会*/

        while (count < 3)
        {
            yesorno = judge();   /*调用函数判断对错*/

            if (yesorno == 1)    /*根据函数的返回结果来输出判断语句*/
            {
                printf("Right!\n");
                score++;
                break;
            }
            else
            {
                printf("Not correct!\n");
                count++;
            }
            if (count == 3)
            {
                wrongs++; /*三次机会都错误则错误加一*/
            }
        }
    }
    printf("You Score is %d\nYou have %d wrongs\n", score * 10, wrongs);
    return 0; /*在十个等式之后统计分数和错误个数*/
}

int random()
{
    return rand() % 10 + 1; /*生成1到10之间的随机数*/
}

char random1()   /*生成随机运算符的函数*/
{
    int ch;
    ch = rand()%4;
    switch (ch)
    {
    case 0:
        return '+';
        break;
    case 1:
        return '-';
        break;
    case 2:
        return '*';
        break;
    case 3:
        return '/';
        break;
    }
}

int judge()
{
    double answer;
    printf("Please input answer: ");
    scanf("%lf", &answer);

    if (fabs(result - answer) < 0.1) /*浮点数储存会失真*/

    {
        return 1;
    }
    else
    {
        return 0;
    }
}